package com.bruce.spring.http.client.factory;

import com.bruce.spring.http.client.util.JsonUtil;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpMethod;
import org.springframework.util.ClassUtils;
import org.springframework.web.service.annotation.HttpExchange;
import org.springframework.web.service.invoker.HttpRequestValues;
import org.springframework.web.service.invoker.HttpServiceArgumentResolver;

import java.util.Map;

/**
 * Created by bruce in 2025/7/3 10:37
 */
public class SimpleObjectGetArgumentResolver implements HttpServiceArgumentResolver {

    @Override
    public boolean resolve(Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
        HttpExchange httpExchange = AnnotatedElementUtils.getMergedAnnotation(parameter.getMethod(), HttpExchange.class);
        if (httpExchange != null && httpExchange.method().equalsIgnoreCase(HttpMethod.GET.name())) {
            boolean primitiveOrWrapper = ClassUtils.isPrimitiveOrWrapper(parameter.getParameterType());
            if (!primitiveOrWrapper && argument != null && !(argument instanceof String)) {
                Map<String, String> map = JsonUtil.toMap(argument);
                map.forEach(requestValues::addRequestParameter);
                return true;
            }
        }
        return false;
    }

}